/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import com.oas.thit._this.profile.depositor.xsd.DepositorProfileResponseBody;
import com.oas.thit._this.xsd.THISBaseDataElementBody;
//import com.oas.thit._this.xsd.THISBaseDataElementBody;
//import com.oas.thit._this.hajj.xsd.HajjResponseBody;
//import com.oas.thit._this.flight.xsd.FlightScheduleResponseBody;
import com.oas.thit.hajj.xsd.RequestBody;
import com.oas.thit.hajj.groupslip.xsd.GroupSlipResponseBody;
import com.oas.thit.hajj.groupslip.xsd.GroupSlipResponseLineItem;
//import com.oas.thit.hajj.hjcheck.xsd.HjCheckRequestBody;
//import com.oas.thit.hajj.hjcheck.xsd.HjCheckResponseBody;
import com.oas.thit.hajj.query.xsd.RegistrationQueryRequestBody;
import com.oas.thit.hajj.query.xsd.RegistrationQueryResponseBody;
import com.oas.thit.hajj.registration.xsd.RegistrationRequestBody;
import com.oas.thit.hajj.registration.xsd.RegistrationResponseBody;
import com.oas.thit.xmlmessage.xsd.Header;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
//import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.DefaultHandler;
import th.model.Pengguna;
import th.model.PermohonanMaklumat;
import th.model.PermohonanMaklumatKeluarga;
import th.model.Haji;
import th.model.Poskod;
//import th.model.PermohonanMaklumatKumpulan;
import th.model.Kumpulan;
//import th.model.PusatKos;
import th.model.SlipPendaftaran;
//import th.stripes.BaseActionBean;
import th.stripes.config.AuthRequired;
import java.util.*;
import java.util.logging.Level;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBElement;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.HttpCache;
//import net.sf.jasperreports.engine.JREmptyDataSource;
//import net.sf.jasperreports.engine.JasperExportManager;
//import net.sf.jasperreports.engine.JasperFillManager;
//import net.sf.jasperreports.engine.JasperPrint;
//import th.manager.UserManager;
import th.manager.HajjManager;
//import th.model.SuratTawaran;
import th.model.InfoAudit;
import th.dao.KodUrusanDAO;
import th.dao.KodBandarDAO;
import th.dao.KodNegeriDAO;
import th.dao.KodWarganegaraDAO;
import th.dao.KodKahwinDAO;
import th.dao.KodTahapPengajianDAO;
import th.dao.KodSektorPekerjaanDAO;
import th.dao.KodPekerjaanDAO;
import th.dao.KodHubunganDAO;
import th.dao.KodStatusDAO;
import th.dao.KodStatusHajiDAO;
import th.dao.PusatKosDAO;
import th.dao.PoskodDAO;
import th.stripes.webservice.WebServiceUtil;
import th.stripes.util.ListUtil;
import th.stripes.BaseActionBean;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.Wizard;
import net.sourceforge.stripes.controller.LifecycleStage;
//import net.sourceforge.stripes.action.StreamingResolution;
import org.apache.log4j.Logger;
import th.config.ableContextListener;
import th.dao.KodJantinaDAO;
import th.dao.THISutilDAO;
import th.manager.ResponseManager;
import th.manager.SejarahTransaksiManager;
import th.model.Deposit;
import th.model.SejarahTransaksi;
import th.model.THISutil;
import th.stripes.util.AccCheckUtil;
import th.stripes.util.DateFormatDetector;
//import th.model.PilihanDestinasi;
//import th.model.SuratPenerbanganJH;
//import th.stripes.Configuration;

/**
 *
 * @author ubuntu
 */

@AuthRequired
@HttpCache(allow = false)
@Wizard(startEvents = "HajjReg_Step1")
@UrlBinding("/haji/registration")
public class HajiRegistrationActionBean extends BaseActionBean{   
           
    private Pengguna pengguna;
    private Haji haji;
    private Deposit deposit;
    //private Kumpulan kumpulan;
    
    private static String SUCCESSFUL = "0000";
    private static String SYSTEM_ERROR = "9999";   
    private static String INVALID_ACC_IC = "9000"; 
    private static String UNSUCCESSFULL = "1000";
    private static String REGISTRATION_FAILED = "1234";
    private static String HAJ_SLIP_MSG_ID = "TPHJSLIPWD01";    
    private static String HEADER_ID_HAJ_SLIP_GROUP = "THSHSLP";
    private static String HAJ_REG_QUERY_MSG_ID = "TPHAJREGWD01";  
    private static String HEADER_ID_HAJ_REGISTRATION_QUERY = "THSHQRY";
    private static String HAJ_REG_MSG_ID = "TPHAJREGWD02";  
    private static String HEADER_ID_HAJ_REGISTRATION = "THSHREG";
    private static String PROFILE_ENQ_MSG_ID = "TPDPPROFWD01";
    private static String HEADER_ID_PROFILE_ENQUIRY = "THSDPQR";
    
    private static String KOD_URUSAN_DAFTAR = "01";
    @Inject HajjManager hajjManager;    
    @Inject private KodUrusanDAO kodUrusanDAO;
    @Inject private KodNegeriDAO kodNegeriDAO;
    @Inject private PoskodDAO poskodDAO;
    @Inject private KodSektorPekerjaanDAO kodSektorPekerjaanDAO;
    @Inject private KodPekerjaanDAO kodPekerjaanDAO;
    @Inject private KodHubunganDAO kodHubunganDAO;
    @Inject private KodStatusDAO kodStatusDAO;
    @Inject private KodBandarDAO kodBandarDAO;
    @Inject private ListUtil listUtil;
    @Inject private WebServiceUtil webUtil;
    @Inject private ResponseManager responseManager;    
    @Inject private SejarahTransaksiManager sejarahTransaksiManager;
    @Inject private THISutilDAO tHISutilDAO;
    @Inject private KodKahwinDAO kodKahwinDAO;
    @Inject private KodWarganegaraDAO kodWarganegaraDAO;
    @Inject private KodTahapPengajianDAO kodTahapPengajianDAO;
    @Inject private KodStatusHajiDAO kodStatusHajiDAO;
    @Inject private PusatKosDAO pusatKosDAO;
    @Inject private KodJantinaDAO kodJantinaDAO;
    
    private static Logger LOG = Logger.getLogger(DepositActionBean.class);
    
    @DefaultHandler
    public Resolution showMainForm() {
        return new JSP("haji/daftarhaji/main_haji.jsp");
    }
    
    @Before(stages={LifecycleStage.BindingAndValidation})
    public void rehydrate() {
        pengguna = getContext().getCurrentUser();        
    }
    
    //-+-+-+-+-+-+-+-+-+-+-+-+-+-PENDAFTARAN HAJI-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//    
    //@HandlesEvent("Step1")
    @HandlesEvent("HajjReg_Step1")
    public Resolution showFormPerakuan () {        
        return new JSP("haji/daftarhaji/perakuan_pendaftar_haji.jsp");
    }
    
    @HandlesEvent("HajjReg_Step2")
    public Resolution showFormPendaftaranHaji () {
        haji = new Haji();
        pengguna = getContext().getCurrentUser();
        if (pengguna != null) { 
            haji.setNoKp(pengguna.getNoKp());
        } 
        return new JSP("haji/daftarhaji/pendaftaran_haji.jsp");
    }    

    @HandlesEvent("HajjReg_Step3")
    public Resolution showPendaftaranHajiDetail () {       
        
        try {
            deposit = getProfileDeposit(haji.getNoAkaun(), haji.getNoKp());
        } catch (ParseException ex) {
            java.util.logging.Logger.getLogger(HajiActionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (deposit != null) {
            
            AccCheckUtil checking = ableContextListener.newInstance(AccCheckUtil.class);
            deposit.setNoKpDari(haji.getNoKp());
            deposit.setNoAkaunDari(haji.getNoAkaun());
            boolean check = checking.checkAcc(deposit, pengguna);

            if (!check) {
                addSimpleError("" + responseManager.getResponseMessage("9000", PROFILE_ENQ_MSG_ID));
                return new JSP("deposit/msg.jsp");
            }
        } else {
            addSimpleError("" + responseManager.getResponseMessage("9000", PROFILE_ENQ_MSG_ID));
                return new JSP("deposit/msg.jsp");
        }
        
        //web service
        String responseCode = "";  
        try { // Call Web Service Operation
            com.oas.thit.hajj.services.HajjServices service = new com.oas.thit.hajj.services.HajjServices();
            com.oas.thit.hajj.services.HajjServicesPortType port = service.getHajjServicesHttpSoap11Endpoint();
            // TODO initialize WS operation arguments here
            com.oas.thit.hajj.query.xsd.RegistrationQueryRequest req = new com.oas.thit.hajj.query.xsd.RegistrationQueryRequest();
            
            com.oas.thit.hajj.query.xsd.ObjectFactory objFactory = new com.oas.thit.hajj.query.xsd.ObjectFactory();
            com.oas.thit.xmlmessage.xsd.ObjectFactory objHeader = new com.oas.thit.xmlmessage.xsd.ObjectFactory();
            Header reqHead = objHeader.createHeader();
            reqHead.setMsgId( objHeader.createHeaderMsgId(HAJ_REG_QUERY_MSG_ID));
            reqHead.setResponseCode( objHeader.createHeaderResponseCode(SUCCESSFUL) );            
            reqHead.setTxid(objHeader.createHeaderTxid( webUtil.getHeaderID(HAJ_REG_QUERY_MSG_ID, HEADER_ID_HAJ_REGISTRATION_QUERY) ));
            
            JAXBElement<Header> jReqHead = objHeader.createEventHeader(reqHead);
            req.setHeader(jReqHead); 
            
            RegistrationQueryRequestBody reqBody = objFactory.createRegistrationQueryRequestBody();
            reqBody.setTHAcctNo(objFactory.createRegistrationQueryRequestBodyTHAcctNo(haji.getNoAkaun()));
            reqBody.setTHICNo(objFactory.createRegistrationQueryRequestBodyTHICNo(haji.getNoKp()));
            
            JAXBElement<RegistrationQueryRequestBody> jReqBody =  objFactory.createRegistrationQueryRequestBody(reqBody);
            req.setBody(jReqBody);
            com.oas.thit.hajj.query.xsd.RegistrationQueryResponse result = port.registrationQueryRequest(req);
            
            Header header = result.getHeader().getValue();
            responseCode = header.getResponseCode().getValue(); 

            if (responseCode.equals(SUCCESSFUL)) {  
                
                RegistrationQueryResponseBody resp = result.getBody().getValue();                
//                PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatByNoAkaun(resp.getTHAcct().getValue().trim(), KOD_URUSAN_DAFTAR);              
                PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatByNoAkaun(haji.getNoAkaun().trim(), KOD_URUSAN_DAFTAR);    
                if(permohonanMaklumat!= null){
                    getContext().getRequest().getSession().setAttribute("hajjRegMemberList", permohonanMaklumat);
                    return new JSP("haji/daftarhaji/pendaftaran_haji_simpan.jsp");
                }else{
                    haji.setNoAkaun(resp.getTHAcct().getValue().trim());
                    haji.setNoKp(resp.getTHICNo().getValue().trim());
                    haji.setNama(resp.getTHName().getValue().trim());
                    haji.setNoTel1(resp.getTHPhoneH().getValue().trim());
                    haji.setNoTel2(resp.getTHPhoneOff().getValue().trim());
                    haji.setNoTel3(resp.getTHMobile().getValue().trim());
                    haji.setFax(resp.getTHFax().getValue().trim());
                    haji.setAlamat1(resp.getTHAddress1().getValue());
                    haji.setAlamat2(resp.getTHAddress2().getValue());
                    haji.setAlamat3(resp.getTHAddress3().getValue());
                    if(resp.getTHPoskod().getValue().substring(0, 5).trim().length()>0){
                        Poskod poskod = poskodDAO.findById(resp.getTHPoskod().getValue().substring(0, 5));
                        haji.setPoskod(poskod.getPoskod());
                        haji.setPoskodDeskripsi(poskod.getNama());
                        haji.setNegeri(poskod.getNegeri());
                    }
                    haji.setSenaraiNegeri(listUtil.getSenaraiNegeri());
                    haji.setSenaraikahwin(listUtil.getSenaraiPerkahwinan());
                    haji.setSenaraiHubungan(listUtil.getSenaraiHubungan());
                    haji.setSenaraipekerjaan(listUtil.getSenaraiPekerjaan());
                    haji.setPekerjaan(kodPekerjaanDAO.findById(resp.getTHEmploy().getValue())); 
                    haji.setSenaraiBandar(listUtil.getSenaraiBandar());
                    haji.setBandar(kodBandarDAO.findById(resp.getTHPlaceofBirth().getValue()));
//                    getContext().getRequest().getSession().setAttribute("haji",haji);
                    return new JSP("haji/daftarhaji/pendaftaran_haji_detail.jsp");
                }    
            }else{
                addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_QUERY_MSG_ID));
            }            
        } catch (Exception ex) {
            LOG.error(ex);
            responseCode = SYSTEM_ERROR;
            addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_QUERY_MSG_ID));
         }
        return new JSP("haji/msg.jsp");
    }
    
    @HandlesEvent("HajjReg_Step4")
    public Resolution savePendaftaranHaji () {      
        pengguna = getContext().getCurrentUser();
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date());
//        Haji hajiInSession = (Haji)getContext().getRequest().getSession().getAttribute("haji");
        getContext().getRequest().getSession().removeAttribute("haji");
        
        PermohonanMaklumat permohonanMaklumat = new PermohonanMaklumat();
        permohonanMaklumat.setUrusan(kodUrusanDAO.findById(KOD_URUSAN_DAFTAR));
        permohonanMaklumat.setNoAkaun(haji.getNoAkaun());
        permohonanMaklumat.setNoPengenalan(haji.getNoKp());
        permohonanMaklumat.setNama(haji.getNama());                      
        //permohonanMaklumat.setPendidikan(hajiInSession.getTahapPengajian());
        //permohonanMaklumat.setPekerjaan(hajiInSession.getPekerjaan());
        //permohonanMaklumat.setPerkahwinan(hajiInSession.getKahwin()); 
        //permohonanMaklumat.setWarganegara(hajiInSession.getWarganegara());        
        permohonanMaklumat.setAlamat1(haji.getAlamat1());
        permohonanMaklumat.setAlamat2(haji.getAlamat2());
        permohonanMaklumat.setAlamat3(haji.getAlamat3());
        permohonanMaklumat.setPoskod(haji.getPoskod());
        permohonanMaklumat.setNegeri(kodNegeriDAO.findById(haji.getNegeri().getCode()));
        permohonanMaklumat.setStatus(kodStatusDAO.findById("0000")); 
        permohonanMaklumat.setNoTel1(haji.getNoTel1());
        permohonanMaklumat.setNoTel2(haji.getNoTel2());
        permohonanMaklumat.setNoTel3(haji.getNoTel3());
        permohonanMaklumat.setNoFax(haji.getFax());
        permohonanMaklumat.setTempatLahir(haji.getBandar()); 
        permohonanMaklumat.setSektorPekerjaan(haji.getSektorPekerjaan());

        permohonanMaklumat.setInfoAudit(ia);
        
        hajjManager.saveMohonMaklumat(permohonanMaklumat);        
        permohonanMaklumat = hajjManager.findMohonMaklumatByNoAkaun(haji.getNoAkaun(), KOD_URUSAN_DAFTAR);
        haji.setMohonMaklumat(permohonanMaklumat);        
        
        getContext().getRequest().getSession().setAttribute("hajjRegMemberList", permohonanMaklumat);
        return new JSP("haji/daftarhaji/pendaftaran_haji_simpan.jsp");
    }
    
    @AuthRequired
    public Resolution showPopupTambahAhli() {
       haji = new Haji();
       String noAkaunPenambah = getContext().getRequest().getParameter("noAkaunPenambah"); 
       haji.setNoAkaunPenambah(noAkaunPenambah);
       return new JSP("/popup/haji/daftar/addnewmember.jsp");
    }
    
    @AuthRequired
    public Resolution showTambahDaftarAhli () {        
        
        String responseCode = "";  
        try { // Call Web Service Operation
            com.oas.thit.hajj.services.HajjServices service = new com.oas.thit.hajj.services.HajjServices();
            com.oas.thit.hajj.services.HajjServicesPortType port = service.getHajjServicesHttpSoap11Endpoint();
            // TODO initialize WS operation arguments here
            com.oas.thit.hajj.query.xsd.RegistrationQueryRequest req = new com.oas.thit.hajj.query.xsd.RegistrationQueryRequest();
            
            com.oas.thit.hajj.query.xsd.ObjectFactory objFactory = new com.oas.thit.hajj.query.xsd.ObjectFactory();
            com.oas.thit.xmlmessage.xsd.ObjectFactory objHeader = new com.oas.thit.xmlmessage.xsd.ObjectFactory();
            Header reqHead = objHeader.createHeader();
            reqHead.setMsgId( objHeader.createHeaderMsgId(HAJ_REG_QUERY_MSG_ID));
            reqHead.setResponseCode( objHeader.createHeaderResponseCode(SUCCESSFUL) );            
            reqHead.setTxid(objHeader.createHeaderTxid( webUtil.getHeaderID(HAJ_REG_QUERY_MSG_ID, HEADER_ID_HAJ_REGISTRATION_QUERY) ));
            
            JAXBElement<Header> jReqHead = objHeader.createEventHeader(reqHead);
            req.setHeader(jReqHead); 
            
            RegistrationQueryRequestBody reqBody = objFactory.createRegistrationQueryRequestBody();
            reqBody.setTHAcctNo(objFactory.createRegistrationQueryRequestBodyTHAcctNo(haji.getNoAkaun()));
            reqBody.setTHICNo(objFactory.createRegistrationQueryRequestBodyTHICNo(haji.getNoKp()));
            
            JAXBElement<RegistrationQueryRequestBody> jReqBody =  objFactory.createRegistrationQueryRequestBody(reqBody);
            req.setBody(jReqBody);
            com.oas.thit.hajj.query.xsd.RegistrationQueryResponse result = port.registrationQueryRequest(req);
            
            Header header = result.getHeader().getValue();
            responseCode = header.getResponseCode().getValue();
            
                if (responseCode.equals(SUCCESSFUL)) {                    
                    RegistrationQueryResponseBody resp = result.getBody().getValue();   
                    haji.setNoAkaun(resp.getTHAcct().getValue().trim());                    
                    haji.setNama(resp.getTHName().getValue());
                    haji.setNoTel1(resp.getTHPhoneH().getValue().trim());
                    haji.setNoTel2(resp.getTHPhoneOff().getValue().trim());
                    haji.setNoTel3(resp.getTHMobile().getValue().trim());
                    haji.setFax(resp.getTHFax().getValue().trim());
                    haji.setAlamat1(resp.getTHAddress1().getValue());
                    haji.setAlamat2(resp.getTHAddress2().getValue());
                    haji.setAlamat3(resp.getTHAddress3().getValue());
                    if(resp.getTHPoskod().getValue().substring(0, 5).trim().length()>0){
                        Poskod poskod = poskodDAO.findById(resp.getTHPoskod().getValue().substring(0, 5));
                        haji.setPoskod(poskod.getPoskod());
                        haji.setPoskodDeskripsi(poskod.getNama());
                        haji.setNegeri(poskod.getNegeri());
                    }
                    haji.setSenaraiNegeri(listUtil.getSenaraiNegeri());
                    haji.setSenaraikahwin(listUtil.getSenaraiPerkahwinan());
                    haji.setSenaraiHubungan(listUtil.getSenaraiHubungan());
                    haji.setSenaraipekerjaan(listUtil.getSenaraiPekerjaan());
                    haji.setPekerjaan(kodPekerjaanDAO.findById(resp.getTHEmploy().getValue()));                    
                    haji.setSenaraiBandar(listUtil.getSenaraiBandar());
                    haji.setBandar(kodBandarDAO.findById(resp.getTHPlaceofBirth().getValue()));
                    getContext().getRequest().getSession().setAttribute("haji",haji); 
                    return new JSP("/popup/haji/daftar/listofmember.jsp");
                }
                addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_QUERY_MSG_ID));            
            } catch (Exception ex) {
            LOG.error(ex);
            responseCode = SYSTEM_ERROR;
            addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_QUERY_MSG_ID));  
        } 
        return new JSP("/popup/haji/daftar/addnewmember.jsp");
        
    } 
    
    @AuthRequired
    public Resolution addNewMember () { 
        
        pengguna = getContext().getCurrentUser();        
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date()); 
        
        Haji hajiInSession = (Haji)getContext().getRequest().getSession().getAttribute("haji");
        getContext().getRequest().getSession().removeAttribute("haji");
        PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatByNoAkaun(haji.getNoAkaunPenambah(), KOD_URUSAN_DAFTAR);
        PermohonanMaklumatKeluarga permohonanMaklumtKeluarga = new PermohonanMaklumatKeluarga();        

        permohonanMaklumtKeluarga.setPermohonan(permohonanMaklumat);
        permohonanMaklumtKeluarga.setNoAkaun(hajiInSession.getNoAkaun());
        permohonanMaklumtKeluarga.setNoPengenalan(hajiInSession.getNoKp());
        permohonanMaklumtKeluarga.setNama(hajiInSession.getNama());
        permohonanMaklumtKeluarga.setAlamat1(hajiInSession.getAlamat1());
        permohonanMaklumtKeluarga.setAlamat2(hajiInSession.getAlamat2());
        permohonanMaklumtKeluarga.setAlamat3(hajiInSession.getAlamat3());
        permohonanMaklumtKeluarga.setPoskod(hajiInSession.getPoskod());
        permohonanMaklumtKeluarga.setNegeri(hajiInSession.getNegeri());
        permohonanMaklumtKeluarga.setNoTel1(hajiInSession.getNoTel1());
        permohonanMaklumtKeluarga.setNoTel2(hajiInSession.getNoTel2());
        permohonanMaklumtKeluarga.setNoTel3(hajiInSession.getNoTel3());
        
        permohonanMaklumtKeluarga.setStatushaji(hajiInSession.getStatusHaji());
        permohonanMaklumtKeluarga.setStatus(hajiInSession.getStatus());        
        permohonanMaklumtKeluarga.setHubungan(kodHubunganDAO.findById(haji.getHubungan().getKod()));
        
        permohonanMaklumtKeluarga.setInfoAudit(ia);        
       
        PermohonanMaklumatKeluarga returnMaklumatKeluarga = hajjManager.saveMohonMaklumatKeluarga(permohonanMaklumtKeluarga);       
        
       
        List maklumatKeluargaList;
        maklumatKeluargaList = permohonanMaklumat.getSenaraiMaklumatKeluarga();
        if(maklumatKeluargaList == null)
            maklumatKeluargaList = new ArrayList();
        maklumatKeluargaList.add(returnMaklumatKeluarga);        
        permohonanMaklumat.setSenaraiMaklumatKeluarga(maklumatKeluargaList); 
            
        getContext().getRequest().getSession().setAttribute("hajjRegMemberList", permohonanMaklumat);        
        //set Message if success, otherwise set message while throwing error
        getContext().getRequest().setAttribute("ReturnMessages", " Maklumat Anda Telah Berjaya Disimpan.");

        return new JSP("/popup/haji/daftar/listofmember.jsp");       
        
    }    
    
    @AuthRequired
    public Resolution deleteMember () { 
        
        pengguna = getContext().getCurrentUser();        
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date());
        
        String id = getContext().getRequest().getParameter("id"); 
        String idPenambah = getContext().getRequest().getParameter("idPenambah");  
        PermohonanMaklumatKeluarga maklumatKeluarga = hajjManager.findMohonMaklumatKeluargaById(Integer.valueOf(id));
        PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatById(Integer.valueOf(idPenambah));
         
        if(maklumatKeluarga!=null){
            maklumatKeluarga.setInfoAudit(ia);
            hajjManager.deleteMohonMaklumatKeluarga(maklumatKeluarga);
        }
        
        List maklumatKeluargaList;
        maklumatKeluargaList = permohonanMaklumat.getSenaraiMaklumatKeluarga();
        if(maklumatKeluargaList == null)
            maklumatKeluargaList = new ArrayList();
        maklumatKeluargaList.remove(maklumatKeluarga);      
        permohonanMaklumat.setSenaraiMaklumatKeluarga(maklumatKeluargaList);       
                
        getContext().getRequest().getSession().setAttribute("hajjRegMemberList", permohonanMaklumat);        
        //set Message if successfull, otherwise set message while throwing error
        getContext().getRequest().getSession().setAttribute("ReturnMessages", " Maklumat Anda Telah Berjaya Disimpan.");

        return new JSP("/haji/ajaxReturn.jsp");    
    }
    
    @AuthRequired
    public Resolution showPopupEditmembers () {
       if(haji == null)
            haji = new Haji();
       String isPrimary = getContext().getRequest().getParameter("isPrimary"); 
       String id = getContext().getRequest().getParameter("id");
       if(isPrimary!=null && isPrimary.equals("true")){            
            haji.setIdMohonMaklumat(Integer.valueOf(id));       
            PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatById(haji.getIdMohonMaklumat());
            haji.setNoAkaun(permohonanMaklumat.getNoAkaun());//hardcoded - for testing purpose
            haji.setNoKp(permohonanMaklumat.getNoPengenalan());
            haji.setNama(permohonanMaklumat.getNama());
            haji.setAlamat1(permohonanMaklumat.getAlamat1());
            haji.setAlamat2(permohonanMaklumat.getAlamat2());
            haji.setAlamat3(permohonanMaklumat.getAlamat3());
            haji.setPoskod(permohonanMaklumat.getPoskod());
            haji.setNoTel1(permohonanMaklumat.getNoTel1());
            haji.setNoTel2(permohonanMaklumat.getNoTel2());
            haji.setNoTel3(permohonanMaklumat.getNoTel3());
            haji.setFax(permohonanMaklumat.getNoFax());
            haji.setBandar(permohonanMaklumat.getTempatLahir());
            haji.setSektorPekerjaan(permohonanMaklumat.getSektorPekerjaan());               
            haji.setNegeri(permohonanMaklumat.getNegeri());
            haji.setHubungan(permohonanMaklumat.getHubungan());
            haji.setSenaraiHubungan(listUtil.getSenaraiHubungan()); 
       }else{            
            haji.setIdMaklumatKeluarga(Integer.valueOf(id));
            PermohonanMaklumatKeluarga permohonanMaklumatKeluarga = hajjManager.findMohonMaklumatKeluargaById(haji.getIdMaklumatKeluarga());
            haji.setNoAkaun(permohonanMaklumatKeluarga.getNoAkaun());//hardcoded - for testing purpose
            haji.setNoKp(permohonanMaklumatKeluarga.getNoPengenalan());
            haji.setNama(permohonanMaklumatKeluarga.getNama());
            haji.setAlamat1(permohonanMaklumatKeluarga.getAlamat1());
            haji.setAlamat2(permohonanMaklumatKeluarga.getAlamat2());
            haji.setAlamat3(permohonanMaklumatKeluarga.getAlamat3());
            haji.setPoskod(permohonanMaklumatKeluarga.getPoskod());
            haji.setNoTel1(permohonanMaklumatKeluarga.getNoTel1());
            haji.setNoTel2(permohonanMaklumatKeluarga.getNoTel2());
            haji.setNoTel3(permohonanMaklumatKeluarga.getNoTel3());             
            haji.setNegeri(permohonanMaklumatKeluarga.getNegeri());
            haji.setHubungan(permohonanMaklumatKeluarga.getHubungan());
            haji.setSenaraiHubungan(listUtil.getSenaraiHubungan());   
       }    
       getContext().getRequest().setAttribute("isPrimary", isPrimary);       
       getContext().getRequest().setAttribute("isEditMode", "true");
       return new JSP("/popup/haji/daftar/listofmember.jsp");
    }
    

    public Resolution saveEditDetail () { 
        
        pengguna = getContext().getCurrentUser();        
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date());
        
        PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatById(haji.getIdMohonMaklumat());        
        permohonanMaklumat.setNoTel1(haji.getNoTel1());
        permohonanMaklumat.setNoTel2(haji.getNoTel2());
        permohonanMaklumat.setNoTel3(haji.getNoTel3());
        permohonanMaklumat.setNoFax(haji.getFax());
        permohonanMaklumat.setSektorPekerjaan(kodSektorPekerjaanDAO.findById(haji.getSektorPekerjaan().getKod()));
        permohonanMaklumat.setTempatLahir(kodBandarDAO.findById(haji.getBandar().getKod()));       
        permohonanMaklumat.setInfoAudit(ia);
        
        hajjManager.saveMohonMaklumat(permohonanMaklumat); 
        
        PermohonanMaklumat returnObj = hajjManager.findMohonMaklumatById(haji.getIdMohonMaklumat());        
        haji.setIdMohonMaklumat(returnObj.getIdMohonMaklumat());
        haji.setNoAkaun(returnObj.getNoAkaun());//hardcoded - for testing purpose
        haji.setNoKp(returnObj.getNoPengenalan());
        haji.setNama(returnObj.getNama());
        haji.setAlamat1(returnObj.getAlamat1());
        haji.setAlamat2(returnObj.getAlamat2());
        haji.setAlamat3(returnObj.getAlamat3());
        haji.setPoskod(returnObj.getPoskod());
        haji.setNoTel1(returnObj.getNoTel1());
        haji.setNoTel2(returnObj.getNoTel2());
        haji.setNoTel3(returnObj.getNoTel3());
        haji.setFax(returnObj.getNoFax());
        haji.setBandar(returnObj.getTempatLahir());
        haji.setSektorPekerjaan(returnObj.getSektorPekerjaan());               
        haji.setNegeri(returnObj.getNegeri());
        haji.setHubungan(returnObj.getHubungan());
        haji.setSenaraiHubungan(listUtil.getSenaraiHubungan());
        //set Message if successfull, otherwise set message while throwing error
        getContext().getRequest().setAttribute("ReturnMessages", "1");
        getContext().getRequest().setAttribute("isPrimary", "true");       
        getContext().getRequest().setAttribute("isEditMode", "true");
        getContext().getRequest().getSession().setAttribute("hajjRegMemberList", returnObj);
        return new JSP("/popup/haji/daftar/listofmember.jsp");      
     
    }
    
    @AuthRequired
    public Resolution saveEditMember () { 
        
        pengguna = getContext().getCurrentUser();
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date());        
     
        PermohonanMaklumatKeluarga permohonanMaklumtKeluarga = hajjManager.findMohonMaklumatKeluargaById(haji.getIdMaklumatKeluarga());       
        permohonanMaklumtKeluarga.setHubungan(kodHubunganDAO.findById(haji.getHubungan().getKod()));       
        permohonanMaklumtKeluarga.setInfoAudit(ia);        
        hajjManager.saveMohonMaklumatKeluarga(permohonanMaklumtKeluarga);        

        PermohonanMaklumatKeluarga permohonanMaklumatKeluarga = hajjManager.findMohonMaklumatKeluargaById(permohonanMaklumtKeluarga.getIdMohonMaklumatKeluarga());
        haji.setNoAkaun(permohonanMaklumatKeluarga.getNoAkaun());//hardcoded - for testing purpose
        haji.setNoKp(permohonanMaklumatKeluarga.getNoPengenalan());
        haji.setNama(permohonanMaklumatKeluarga.getNama());
        haji.setAlamat1(permohonanMaklumatKeluarga.getAlamat1());
        haji.setAlamat2(permohonanMaklumatKeluarga.getAlamat2());
        haji.setAlamat3(permohonanMaklumatKeluarga.getAlamat3());
        haji.setPoskod(permohonanMaklumatKeluarga.getPoskod());
        haji.setNoTel1(permohonanMaklumatKeluarga.getNoTel1());
        haji.setNoTel2(permohonanMaklumatKeluarga.getNoTel2());
        haji.setNoTel3(permohonanMaklumatKeluarga.getNoTel3());             
        haji.setNegeri(permohonanMaklumatKeluarga.getNegeri());
        haji.setHubungan(permohonanMaklumatKeluarga.getHubungan());
        haji.setSenaraiHubungan(listUtil.getSenaraiHubungan());  
        
        PermohonanMaklumat permohonanMaklumat = permohonanMaklumtKeluarga.getPermohonan();                
        permohonanMaklumat = hajjManager.findMohonMaklumatById(permohonanMaklumat.getIdMohonMaklumat());
        //set Message if successfull, otherwise set message while throwing error
        getContext().getRequest().setAttribute("ReturnMessages", "1");
        getContext().getRequest().setAttribute("isEditMode", "true");
        getContext().getRequest().getSession().setAttribute("hajjRegMemberList", permohonanMaklumat);
        return new JSP("/popup/haji/daftar/listofmember.jsp");  
    }
    
    @HandlesEvent("HajjReg_Step5")
    public Resolution submitPendaftaranHaji () {      
        pengguna = getContext().getCurrentUser();
        if (pengguna != null) {           
            haji.setNoKp(pengguna.getNoKp());
            haji.setNama(pengguna.getNama());
        } 
        //log
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date());
        
        PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatByNoAkaun(haji.getNoAkaun(), KOD_URUSAN_DAFTAR);
        List permohonanMaklumatKeluargaList  = null;
        if(permohonanMaklumat!= null)
            permohonanMaklumatKeluargaList = permohonanMaklumat.getSenaraiMaklumatKeluarga();

        String responseCode = "";
        SejarahTransaksi sejarahTransaksi = new SejarahTransaksi();
        SimpleDateFormat d = new SimpleDateFormat("d MMM yyyy");
        SimpleDateFormat t = new SimpleDateFormat("hh:mm:ss");
        try { // Call Web Service Operation
            com.oas.thit.hajj.services.HajjServices service = new com.oas.thit.hajj.services.HajjServices();
            com.oas.thit.hajj.services.HajjServicesPortType port = service.getHajjServicesHttpSoap11Endpoint();
            com.oas.thit.hajj.registration.xsd.RegistrationRequest req = new com.oas.thit.hajj.registration.xsd.RegistrationRequest();
            com.oas.thit.hajj.registration.xsd.ObjectFactory objFactory = new com.oas.thit.hajj.registration.xsd.ObjectFactory();            
            com.oas.thit._this.xsd.ObjectFactory _objFactory = new com.oas.thit._this.xsd.ObjectFactory();
            
            req.setHeader( webUtil.createHeader(HAJ_REG_MSG_ID, HEADER_ID_HAJ_REGISTRATION) );
            
            RegistrationRequestBody reqBody = objFactory.createRegistrationRequestBody();
            reqBody.setTHAcct01(objFactory.createRegistrationRequestBodyTHAcct01(permohonanMaklumat.getNoAkaun()));
            reqBody.setTHICNo01(objFactory.createRegistrationRequestBodyTHICNo01(permohonanMaklumat.getNoPengenalan()));
            reqBody.setTHMobile01(objFactory.createRegistrationRequestBodyTHMobile01(permohonanMaklumat.getNoTel3()));
            reqBody.setTHPhoneOffice01(objFactory.createRegistrationRequestBodyTHPhoneOffice01(permohonanMaklumat.getNoTel2()));
            reqBody.setTHPhoneHome01(objFactory.createRegistrationRequestBodyTHPhoneHome01(permohonanMaklumat.getNoTel1()));
            reqBody.setTHFax01(objFactory.createRegistrationRequestBodyTHFax01(permohonanMaklumat.getNoFax()));
            reqBody.setTHPlaceofBirth01(objFactory.createRegistrationRequestBodyTHPlaceofBirth01(permohonanMaklumat.getTempatLahir().getKod()));
            reqBody.setTHEmploy01(objFactory.createRegistrationRequestBodyTHEmploy01(permohonanMaklumat.getSektorPekerjaan().getKod()));
            
            if(permohonanMaklumatKeluargaList.size()>0){              
                for(int j=0; j<permohonanMaklumat.getSenaraiMaklumatKeluarga().size(); j++){
                    if(j==permohonanMaklumatKeluargaList.size())
                        break;
                    PermohonanMaklumatKeluarga permohonanMaklumatKeluarga = (PermohonanMaklumatKeluarga)permohonanMaklumat.getSenaraiMaklumatKeluarga().get(j); 
                    if(j==0){
                        reqBody.setTHAcct02(objFactory.createRegistrationRequestBodyTHAcct02(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo02(objFactory.createRegistrationRequestBodyTHICNo02(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship02(objFactory.createRegistrationRequestBodyTHRelationship02(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth02(objFactory.createRegistrationRequestBodyTHPlaceofBirth02(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==1){
                        reqBody.setTHAcct03(objFactory.createRegistrationRequestBodyTHAcct03(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo03(objFactory.createRegistrationRequestBodyTHICNo03(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship03(objFactory.createRegistrationRequestBodyTHRelationship03(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth03(objFactory.createRegistrationRequestBodyTHPlaceofBirth03(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==2){
                        reqBody.setTHAcct04(objFactory.createRegistrationRequestBodyTHAcct04(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo04(objFactory.createRegistrationRequestBodyTHICNo04(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship04(objFactory.createRegistrationRequestBodyTHRelationship04(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth04(objFactory.createRegistrationRequestBodyTHPlaceofBirth04(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==3){
                        reqBody.setTHAcct05(objFactory.createRegistrationRequestBodyTHAcct05(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo05(objFactory.createRegistrationRequestBodyTHICNo05(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship05(objFactory.createRegistrationRequestBodyTHRelationship05(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth05(objFactory.createRegistrationRequestBodyTHPlaceofBirth05(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==4){
                        reqBody.setTHAcct06(objFactory.createRegistrationRequestBodyTHAcct06(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo06(objFactory.createRegistrationRequestBodyTHICNo06(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship06(objFactory.createRegistrationRequestBodyTHRelationship06(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth06(objFactory.createRegistrationRequestBodyTHPlaceofBirth06(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==5){
                        reqBody.setTHAcct07(objFactory.createRegistrationRequestBodyTHAcct07(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo07(objFactory.createRegistrationRequestBodyTHICNo07(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship07(objFactory.createRegistrationRequestBodyTHRelationship07(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth07(objFactory.createRegistrationRequestBodyTHPlaceofBirth07(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==6){
                        reqBody.setTHAcct08(objFactory.createRegistrationRequestBodyTHAcct08(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo08(objFactory.createRegistrationRequestBodyTHICNo08(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship08(objFactory.createRegistrationRequestBodyTHRelationship08(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth08(objFactory.createRegistrationRequestBodyTHPlaceofBirth08(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==7){
                        reqBody.setTHAcct09(objFactory.createRegistrationRequestBodyTHAcct09(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo09(objFactory.createRegistrationRequestBodyTHICNo09(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship09(objFactory.createRegistrationRequestBodyTHRelationship09(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth09(objFactory.createRegistrationRequestBodyTHPlaceofBirth09(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                    if(j==8){
                        reqBody.setTHAcct10(objFactory.createRegistrationRequestBodyTHAcct10(permohonanMaklumatKeluarga.getNoAkaun()));
                        reqBody.setTHICNo10(objFactory.createRegistrationRequestBodyTHICNo10(permohonanMaklumatKeluarga.getNoPengenalan()));
                        reqBody.setTHRelationship10(objFactory.createRegistrationRequestBodyTHRelationship10(permohonanMaklumatKeluarga.getHubungan().getKod()));
                        reqBody.setTHPlaceofBirth10(objFactory.createRegistrationRequestBodyTHPlaceofBirth10(permohonanMaklumatKeluarga.getHubungan().getKod()));
                    }
                }                
            }
            
            JAXBElement<RegistrationRequestBody> jReqBody =  objFactory.createRegistrationRequestBody(reqBody);
            req.setBody(jReqBody);
            com.oas.thit.hajj.registration.xsd.RegistrationResponse result = port.hajjRegistration(req);

            Header header = result.getHeader().getValue();
            responseCode = header.getResponseCode().getValue();
            sejarahTransaksi.setPengguna(pengguna);
            sejarahTransaksi.setTrhMasuk(new Date());
            sejarahTransaksi.setUrusan(kodUrusanDAO.findById(HAJ_REG_MSG_ID));
            sejarahTransaksi.setIdSessi(getContext().getRequest().getSession().getId());
            sejarahTransaksi.setStatus(kodStatusDAO.findById(responseCode));
            sejarahTransaksi.setNilaiDalaman1(header.getTxid().getValue().trim());
            RegistrationResponseBody resp = result.getBody().getValue();
    
                if(responseCode.equals(SUCCESSFUL)){
                    try { 
                        com.oas.thit.hajj.services.HajjServices service2 = new com.oas.thit.hajj.services.HajjServices();
                        com.oas.thit.hajj.services.HajjServicesPortType port2 = service2.getHajjServicesHttpSoap11Endpoint();
                        // TODO initialize WS operation arguments here
                        com.oas.thit.hajj.xsd.Request req2 = new com.oas.thit.hajj.xsd.Request();               

                        com.oas.thit.hajj.xsd.ObjectFactory objFactory2 = new com.oas.thit.hajj.xsd.ObjectFactory();
                        com.oas.thit.xmlmessage.xsd.ObjectFactory objHeader = new com.oas.thit.xmlmessage.xsd.ObjectFactory();
                        Header reqHead = objHeader.createHeader();
                        reqHead.setMsgId( objHeader.createHeaderMsgId(HAJ_SLIP_MSG_ID));
                        reqHead.setResponseCode( objHeader.createHeaderResponseCode(SUCCESSFUL) );            
                        reqHead.setTxid(objHeader.createHeaderTxid( webUtil.getHeaderID(HAJ_SLIP_MSG_ID, HEADER_ID_HAJ_SLIP_GROUP) ));

                        JAXBElement<Header> jReqHead2 = objHeader.createEventHeader(reqHead);
                        req2.setHeader(jReqHead2); 

                        RequestBody reqBody2 = objFactory2.createRequestBody();
                        reqBody2.setTHAccountNo(objFactory2.createRequestBodyTHAccountNo(permohonanMaklumat.getNoAkaun()));
                        reqBody2.setTHIcNo(objFactory2.createRequestBodyTHIcNo(permohonanMaklumat.getNoPengenalan()));

                        JAXBElement<RequestBody> jReqBody2 =  objFactory2.createRequestBody(reqBody2);
                        req2.setBody(jReqBody2);

                        // TODO process result here
                        com.oas.thit.hajj.groupslip.xsd.GroupSlipResponse result2 = port2.groupSlip(req2);                
                        Header header2 = result2.getHeader().getValue();
                        String responseCode2 = header2.getResponseCode().getValue();                
                            if (responseCode2.equals(SUCCESSFUL)) {
                                GroupSlipResponseBody resp2 = result2.getBody().getValue();                                
                                if(resp2.getTHHajRefNo().getValue()== null || resp2.getTHHajRefNo().getValue().trim().equals(""))
                                    haji.setNoRujukan("Tiada");
                                else
                                    haji.setNoRujukan(resp2.getTHHajRefNo().getValue());
                                haji.setTahunPeruntukan(resp2.getTHHajjYear().getValue());
                                haji.setTahunPeruntukanHijrah(resp2.getTHHajjYearI().getValue());
                                DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                                if(resp2.getTHRegDate().getValue() != null)
                                    haji.setTarikhDaftar(format.parse(resp2.getTHRegDate().getValue().trim()));
                                THISutil tHISutil = tHISutilDAO.findById("101");  
                                haji.setNoTelTHCC(tHISutil.getDeskripsi().toString());
                                List<GroupSlipResponseLineItem> items  = resp2.getLineItems();
                                List<SlipPendaftaran> senaraiAhli = new ArrayList<SlipPendaftaran>();
                                for ( GroupSlipResponseLineItem item : items ) {
                                    SlipPendaftaran sp = new SlipPendaftaran();                        
                                    sp.setNama(item.getTHName().getValue());
                                    sp.setNoAkaun(item.getTHAcct().getValue());
                                    sp.setNoDaftarHaji(item.getTHHajRegNo().getValue());
                                    sp.setPejabatPengendali(item.getTHOffice().getValue());                        
                                    senaraiAhli.add(sp);
                                }                                
                                haji.setSenaraiDaftarAhli(senaraiAhli);
                                sejarahTransaksiManager.saveSejarahTransaksi(sejarahTransaksi);
                                getContext().getRequest().getSession().setAttribute("haji", haji);
                                return new JSP("haji/daftarhaji/cetak_slip_pendaftaran_haji_detail.jsp");
                            }  
                        } 
                        catch (Exception ex) {
                        LOG.error(ex);
                        responseCode = SYSTEM_ERROR;
                        addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_MSG_ID));
                    }
            }else if(responseCode.equals(UNSUCCESSFULL)){
                if(!resp.getTHResponseCode01().getValue().trim().equals(SUCCESSFUL)){                    
                    responseCode = REGISTRATION_FAILED;
                    addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_MSG_ID));
                    //addSimpleError("ANDA TIDAK LAYAK MENDAFTAR HAJI. UNTUK MAKLMUAT LANJUT SILA HADIR KE MANA-MANA KAUNTER TABUNG HAJI");
                    return new JSP("haji/msg.jsp"); 
                }
                    
                permohonanMaklumat.setStatus(kodStatusDAO.findById(resp.getTHResponseCode01().getValue())); //hardcode
                if(permohonanMaklumatKeluargaList!=null && 
                        permohonanMaklumatKeluargaList.size()>0){
                    List stsMaklumatKeluargaList = new ArrayList();
                    for(int j=0; j<permohonanMaklumat.getSenaraiMaklumatKeluarga().size(); j++){                
                                if(j==permohonanMaklumatKeluargaList.size())
                                    break;
                                PermohonanMaklumatKeluarga permohonanMaklumatKeluarga = (PermohonanMaklumatKeluarga)permohonanMaklumat.getSenaraiMaklumatKeluarga().get(j); 
                                if(j==0)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode02().getValue()));
                                else if(j==1)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode03().getValue()));
                                else if(j==2)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode04().getValue()));
                                else if(j==3)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode05().getValue()));
                                else if(j==4)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode06().getValue()));
                                else if(j==5)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode07().getValue()));
                                else if(j==6)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode08().getValue()));
                                else if(j==7)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode09().getValue()));
                                else if(j==8)
                                    permohonanMaklumatKeluarga.setStatus(kodStatusDAO.findById(resp.getTHResponseCode10().getValue()));
                                stsMaklumatKeluargaList.add(permohonanMaklumatKeluarga);
                    }
                    permohonanMaklumat.setSenaraiMaklumatKeluarga(stsMaklumatKeluargaList);
                }
                getContext().getRequest().getSession().setAttribute("pindaSenarai", "true");
                getContext().getRequest().getSession().setAttribute("hajjRegMemberList", permohonanMaklumat);
                addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_MSG_ID));
                return new JSP("haji/daftarhaji/pendaftaran_haji_simpan.jsp");
            }else if(responseCode.equals(SYSTEM_ERROR)){
                responseCode = SYSTEM_ERROR;
                addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_MSG_ID));
            }      
        } catch (Exception ex) {
            LOG.error(ex);
            responseCode = SYSTEM_ERROR;
            addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_MSG_ID));
        } 
        sejarahTransaksiManager.saveSejarahTransaksi(sejarahTransaksi);
        return new JSP("haji/daftarhaji/pendaftaran_haji_simpan.jsp"); 
    }
    
    public Resolution batalPendaftaranHaji () {
        pengguna = getContext().getCurrentUser();
        if (pengguna != null) {           
            haji.setNoKp(pengguna.getNoKp());
            haji.setNama(pengguna.getNama());
        } 
        //log
        InfoAudit ia = new InfoAudit();
        ia.setDimasukOleh(pengguna);
        ia.setTarikhMasuk(new Date());
        try { // Call Web Service Operation
            PermohonanMaklumat permohonanMaklumat = hajjManager.findMohonMaklumatByNoAkaun(haji.getNoAkaun(), KOD_URUSAN_DAFTAR);
            if(permohonanMaklumat!=null)
                hajjManager.deleteMohonMaklumat(permohonanMaklumat);            
            addSimpleMessage("PENDAFTARAN BERJAYA DIBATALKAN");
            
        } catch (Exception ex) {
            LOG.error(ex);
            String responseCode = SYSTEM_ERROR;
            addSimpleError("" + responseManager.getResponseMessage(responseCode, HAJ_REG_MSG_ID));
        } 
        return new JSP("haji/msg.jsp"); 
    }

    /**
     * Get profile from deposit
     * @param noAkaun
     * @param noKP
     * @return 
     * 
     */
    public Deposit getProfileDeposit(String noAkaun, String noKP) throws ParseException {
       
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String responseCode = "";
        //Deposit deposit = null;
        
        com.oas.thit._this.services.THISServices service = new com.oas.thit._this.services.THISServices();
        com.oas.thit._this.services.THISServicesPortType port = service.getTHISServicesHttpSoap11Endpoint();
        com.oas.thit._this.xsd.THISRequest reqDeposit = new com.oas.thit._this.xsd.THISRequest();
        com.oas.thit._this.xsd.ObjectFactory objFactoryDeposit = new com.oas.thit._this.xsd.ObjectFactory();
        reqDeposit.setHeader( webUtil.createHeader(PROFILE_ENQ_MSG_ID, HEADER_ID_PROFILE_ENQUIRY) );
        THISBaseDataElementBody reqBodyDeposit = objFactoryDeposit.createTHISBaseDataElementBody();
        
        reqBodyDeposit.setTHAcct(objFactoryDeposit.createTHISBaseDataElementBodyTHAcct(noAkaun));
        reqBodyDeposit.setTHICNo(objFactoryDeposit.createTHISBaseDataElementBodyTHICNo(noKP));

        JAXBElement<THISBaseDataElementBody> jReqBodyDeposit =  objFactoryDeposit.createTHISRequestBody(reqBodyDeposit);
        reqDeposit.setBody(jReqBodyDeposit);
        
         /*
          * Query for deposit : profile
          */
            com.oas.thit._this.profile.depositor.xsd.DepositorProfileResponse resultDeposit = port.depositorProfileQuery(reqDeposit);
            Header headerDeposit = resultDeposit.getHeader().getValue();
            responseCode = headerDeposit.getResponseCode().getValue();
            if (responseCode.equals(SUCCESSFUL)) {
                 DepositorProfileResponseBody respDeposit = resultDeposit.getBody().getValue();
                 if (respDeposit != null) { 
                    deposit = new Deposit();
                    deposit.setAlamat1(respDeposit.getTHAddress1().getValue());
                    deposit.setAlamat2(respDeposit.getTHAddress2().getValue());
                    deposit.setAlamat3(respDeposit.getTHAddress3().getValue());
                    deposit.setBangsa(respDeposit.getTHEthnic().getValue());
                    deposit.setEmail(respDeposit.getTHemail().getValue());
                    deposit.setNoAkaun(respDeposit.getTHAcct().getValue().trim());
                    deposit.setNoBuku(respDeposit.getTHPassbookNo().getValue());
                    deposit.setNoKp(respDeposit.getTHICNo().getValue().trim());
                    deposit.setNoTel1(respDeposit.getTHPhoneHome().getValue());
                    deposit.setNoTel2(respDeposit.getTHPhoneMobile().getValue());
                    deposit.setNoTel3(respDeposit.getTHPhoneOffice().getValue());
                    deposit.setPoskod(respDeposit.getTHPoskod().getValue());
                    deposit.setNama(respDeposit.getTHName().getValue());
                    
                    if (respDeposit.getTHGender() != null) {
                        deposit.setJantina(kodJantinaDAO.findById(respDeposit.getTHGender().getValue().trim()));
                    }
                    if  (respDeposit.getTHMarital() != null) {
                        deposit.setKahwin(kodKahwinDAO.findById(respDeposit.getTHMarital().getValue().trim()));
                    }
                   
                    if (respDeposit.getTHState() != null) {
                        deposit.setNegeri(kodNegeriDAO.findById(respDeposit.getTHState().getValue().trim()));
                    }
                     if (respDeposit.getTHEmploy() != null) {
                        deposit.setPekerjaan(kodPekerjaanDAO.findById(respDeposit.getTHEmploy().getValue().trim()));
                    }
                    if (respDeposit.getTHNationality() != null) {
                        deposit.setWarganegara(kodWarganegaraDAO.findById(respDeposit.getTHNationality().getValue().trim()));
                    }
                    
                    /*
                     * Set date
                     */
                    String df = new DateFormatDetector().getDateFormat(respDeposit.getTHDOB().getValue());
                    sdf = new SimpleDateFormat(df);
                    deposit.setTarikhLahir(sdf.parse(respDeposit.getTHDOB().getValue()));
                 }
            }
        return deposit;
    }    

    public Pengguna getPengguna() {
        return pengguna;
    }

    public void setPengguna(Pengguna pengguna) {
        this.pengguna = pengguna;
    }

    public Haji getHaji() {
        return haji;
    }

    public void setHaji(Haji haji) {
        this.haji = haji;
    }
   
  
}
